'''
Contains all the functions needed to create a new CSN claim, or submit
all Columbus City Schools claims.
'''
from copy import deepcopy as copy_deepcopy
from json import load as json_load, dump as json_dump
import PySimpleGUI as sg
from warranty_manager.gui.confirmation import get_confirmation


def convert_set_to_list(obj):
    '''To convert all sets in the claim data to lists, before converting it to JSON.'''
    if isinstance(obj, set):
        return list(obj)


def convert_claim_lists_to_sets(claim_info):
    '''
    Converts all lists in the claim dictionary to sets, before loading them
    from the JSON file.
    '''
    for k, v, in claim_info.items():

        if isinstance(v, list):
            claim_info[k] = set(v)

    return claim_info


def load_claim_info(default_claim_info, window):
    '''Loads the saved claim information from a file.'''
    default_claim_info_copy = copy_deepcopy(default_claim_info)

    if get_confirmation('load the current CSN claim information from your computer'):
        try:
            window.load_from_disk('csn_text_input_information.pickle')

            with open('csn_chromebook_parts_and_notes_information.json', 'r') as f:
                chromebook_issue_notes_and_parts \
                = convert_claim_lists_to_sets( json_load(f) )

            default_claim_info_copy.update(chromebook_issue_notes_and_parts)

            if len(default_claim_info_copy['notes']) > 0:
                window['-ADD_NOTES-'].update(text='Add More Notes')

            if len(default_claim_info_copy['part numbers']) > 0:
                window['-ADD_PART_NUMBERS-'].update(text='Add More Parts')

            sg.popup(
                'The claim information files have been successfully loaded!',
                title='Info Message'
            )
            return default_claim_info_copy
        except Exception as err:
            print(f'There was an error loading the claim files: {err}.')
            return default_claim_info_copy
    else:
        sg.popup(
            'No CSN claim information files have been loaded.',
            title='Error Message'
        )
        return default_claim_info_copy


def save_claim_info(claim_info, window):
    '''Saves the current claim information to a file.'''
    if get_confirmation('save the current CSN claim information to your computer'):
        chromebook_issue_notes_and_parts = {
            'notes': claim_info['notes'],
            'part numbers': claim_info['part numbers']
        }

        try:
            window.SaveToDisk('csn_text_input_information.pickle')

            with open('csn_chromebook_parts_and_notes_information.json', 'w') as f:
                json_dump(
                    chromebook_issue_notes_and_parts, f, default=convert_set_to_list
                )

            return sg.popup(
                'The claim information files have been successfully created!',
                title='Info Message'
            )
        except Exception as err:
            print(f'There was an error creating the claim files: {err}.')
    else:
        sg.popup(
            'No CSN claim information files have been created.',
            title='Error Message'
        )


def replace_keys():
    '''
    Replaces all the keys for the specified keyboard issue in the claim.
    '''
